﻿using System.Runtime.InteropServices;

namespace SMU_4x1
{
    //--------------------------------------------------------------
    #region 列挙型
    /// <summary>
    /// タイマー識別ID
    /// </summary>
    public enum EnumTimerId
    {
        /// <summary>検査時間</summary>
        INSP = 0,
        /// <summary>EOT</summary>
        EOT,
        /// <summary>WAIT関数用</summary>
        WAIT,
        Timer,
        MAX
    }
    #endregion
    //--------------------------------------------------------------
    /// <summary>
    /// タイマー関係定数宣言クラス
    /// </summary>
    class CTimerConst
    {
        //--------------------------------------------------------------
        #region 定数
        #endregion
        //--------------------------------------------------------------
    }
    //--------------------------------------------------------------
    /// <summary>
    /// タイマークラス
    /// </summary>
    class CTimer
    {
        //--------------------------------------------------------
        #region 変数宣言
        /// <summary>周波数</summary>
        long m_lpFrequency;
        /// <summary>開始時間保存配列</summary>
        long[] m_lStartTime;
        #endregion
        //--------------------------------------------------------
        #region DLL変数
        [DllImport("winmm.dll")]
        static extern int timeBeginPeriod(int uPeriod);
        [DllImport("winmm.dll")]
        static extern int timeEndPeriod(int uPeriod);

        [DllImport("kernel32.dll")]
        static extern bool QueryPerformanceFrequency(out long lpFrequency);
        [DllImport("kernel32.dll")]
        static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
        #endregion
        //--------------------------------------------------------
        /// <summary>コンストラクタ</summary>
        public CTimer()
        {
            // 分辨率设置为1毫秒
            timeBeginPeriod(1);

            m_lStartTime = new long[(int)EnumTimerId.MAX];
        }
        //--------------------------------------------------------
        /// <summary>デストラクタ</summary>
        ~CTimer()
        {
        }
        //--------------------------------------------------------
        /// <summary>
        /// 初期化処理
        /// </summary>
        public bool Init()
        {
            if (QueryPerformanceFrequency(out m_lpFrequency) == false)
            {
                return false;
            }

            return true;
        }
        //--------------------------------------------------------
        /// <summary>
        /// 解放処理
        /// </summary>
        public void Close()
        {
            // 分解能をクリア
            timeEndPeriod(1);
        }
        //--------------------------------------------------------
        /// <summary>
        /// 指定時間待つ
        /// </summary>
        public void WaitTime(long Microseconds)
        {
            StartTime(EnumTimerId.WAIT);
            while (true)
            {
                if (LapTime(EnumTimerId.WAIT) > Microseconds)
                {
                    break;
                }
            }
        }
        //--------------------------------------------------------
        /// <summary>
        /// 時間計測開始
        /// </summary>
        public void StartTime(EnumTimerId enId)
        {
            QueryPerformanceCounter(out m_lStartTime[(int)enId]);
        }
        //--------------------------------------------------------
        /// <summary>
        /// 時間経過取得(usec)
        /// </summary>
        public long LapTime(EnumTimerId enId)
        {
            long lEndTime;

            QueryPerformanceCounter(out lEndTime);

            return (lEndTime - m_lStartTime[(int)enId]) * 1000000 / m_lpFrequency;
        }
        //--------------------------------------------------------
    }
}

